package vip.liux.front.application.service.coupon;

import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import vip.liux.contracts.infrastructure.automap.IgnoreEntity;
import vip.liux.contracts.infrastructure.automap.MapMapper;
import vip.liux.contracts.infrastructure.automap.MapStructConfig;
import vip.liux.contracts.models.coupon.Coupon;
import vip.liux.contracts.models.coupon.UserCoupon;
import vip.liux.contracts.shared.value.ReceivedRecord;

import java.util.Map;

@Mapper(config = MapStructConfig.class, uses = {MapMapper.class})
public interface AutoMapperProfile {

    AutoMapperProfile INSTANCE = Mappers.getMapper(AutoMapperProfile.class);

    @IgnoreEntity
    @Mapping(target = "received", ignore = true)
    @Mapping(target = "state", ignore = true)
    @Mapping(target = "quantityAvailable", expression = "java(source.getReceiveNum() - record.getOrDefault(source.getCouponNo(), ReceivedRecord.empty(source.getCouponNo())).getReceivedCount())")
    ShopCouponDto toShopCouponDto(Coupon source, @Context Map<String, ReceivedRecord> record);

    @Mapping(target = "id", source = "source.id")
    @Mapping(target = "state", ignore = true)
    @Mapping(target = "creationTime", source = "source.creationTime")
    UserCouponDto toUserCoupon(UserCoupon source, Coupon coupon);
}
